package com.dwjm.shop.Jbp.client;

import com.dwjm.shop.Jbp.Goods.Goods;
import com.dwjm.shop.Jbp.Goods.GoodsDao;
import com.dwjm.shop.Jbp.Goods.GoodsDaoImpl;
import com.dwjm.shop.Jbp.utils.CommonResult;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/loadGoods")
public class ConsumerAllGoodHandler extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 设置编码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        ConsumerDao consumerDao = new ConsumerDaoImpl();

        List<Goods> allGoods = consumerDao.getAllConsumer();
        if (allGoods == null) {
            resp.getWriter().write(new Gson().toJson(CommonResult.fail(500, "数据库查询失败")));
            return;
        }
        Gson gson = new Gson();

        CommonResult<List<Goods>> temp = null;
        if (allGoods.size() > 0) {
            temp = CommonResult.success(allGoods);
        } else {
            temp = CommonResult.fail(500, "没有对应数据");
        }
        String s = gson.toJson(temp);
        System.out.println(">>>>>>>>>>." + s);
        resp.getWriter().write(s);

    }

}
